package com.ruoyi.web.controller.byw;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.PositionVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BywPositionType;
import com.ruoyi.system.service.IBywPositionTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 职位字典Controller
 * 
 * @author ruoyi
 * @date 2024-06-01
 */
@RestController
@RequestMapping("/byw/position")
public class BywPositionController extends BaseController
{
    @Autowired
    private IBywPositionTypeService bywPositionService;

    /**
     * 查询职位字典列表
     */
    @PreAuthorize("@ss.hasPermi('byw:position:list')")
    @GetMapping("/list")
    public AjaxResult list(BywPositionType bywPositionType)
    {
        List<PositionVo> positionVoList = new ArrayList<>();
        List<BywPositionType> list = bywPositionService.selectBywPositionList(bywPositionType);
        for (BywPositionType position : list){
            if (position.getParentId() == 0){
                PositionVo positionVo = new PositionVo();
                positionVo.setId(position.getId());
                positionVo.setParentId(position.getParentId());
                positionVo.setPositionName(position.getPositionName());
                List<PositionVo> positionVoList2 = new ArrayList<>();
                for (BywPositionType position1 : list){
                    if (position.getId().equals(position1.getParentId())){
                        PositionVo positionVo2 = new PositionVo();
                        positionVo2.setId(position1.getId());
                        positionVo2.setParentId(position1.getParentId());
                        positionVo2.setPositionName(position1.getPositionName());
                        positionVoList2.add(positionVo2);
                    }
                }
                positionVo.setPositionList(positionVoList2);
                positionVoList.add(positionVo);
            }
        }
        return success(positionVoList);
    }

    @PreAuthorize("@ss.hasPermi('byw:position:list')")
    @GetMapping("/list-position")
    public AjaxResult listPosition(BywPositionType bywPositionType)
    {
        List<BywPositionType> list = bywPositionService.selectBywPositionList(bywPositionType);
        List<PositionVo> positionVoList = new ArrayList<>();

        for (BywPositionType position : list){
            if (position.getParentId() != 0){
                PositionVo positionVo = new PositionVo();
                positionVo.setId(position.getId());
                positionVo.setParentId(position.getParentId());
                positionVo.setPositionName(position.getPositionName());
                positionVoList.add(positionVo);
            }
        }
        return success(positionVoList);
    }

    /**
     * 导出职位字典列表
     */
    @PreAuthorize("@ss.hasPermi('byw:position:export')")
    @Log(title = "职位字典", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BywPositionType bywPositionType)
    {
        List<BywPositionType> list = bywPositionService.selectBywPositionList(bywPositionType);
        ExcelUtil<BywPositionType> util = new ExcelUtil<BywPositionType>(BywPositionType.class);
        util.exportExcel(response, list, "职位字典数据");
    }

    /**
     * 获取职位字典详细信息
     */
    @PreAuthorize("@ss.hasPermi('byw:position:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bywPositionService.selectBywPositionById(id));
    }

    /**
     * 新增职位字典
     */
    @PreAuthorize("@ss.hasPermi('byw:position:add')")
    @Log(title = "职位字典", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BywPositionType bywPositionType)
    {
        return toAjax(bywPositionService.insertBywPosition(bywPositionType));
    }

    /**
     * 修改职位字典
     */
    @PreAuthorize("@ss.hasPermi('byw:position:edit')")
    @Log(title = "职位字典", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BywPositionType bywPositionType)
    {
        return toAjax(bywPositionService.updateBywPosition(bywPositionType));
    }

    /**
     * 删除职位字典
     */
    @PreAuthorize("@ss.hasPermi('byw:position:remove')")
    @Log(title = "职位字典", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bywPositionService.deleteBywPositionByIds(ids));
    }
}
